package com.home.truc.code.components;

import javax.swing.JLabel;

import com.home.Common.Common;
import com.home.Entity.Ban;
import com.home.Entity.TTGoiMon;
import com.home.duy.code.DAO.BanDAO;
import com.home.duy.code.DAO.TTGoiMonDAO;
import com.home.truc.code.components.MyJPanelKhuVucBan;
import com.home.truc.code.process.MyControl;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.event.MenuKeyListener;
import javax.swing.event.MenuKeyEvent;

public class MyItemThongTinGoiMon extends JLabel {

	private static final long serialVersionUID = 1L;
	
	private TTGoiMon entry = null;
	private JPanel panelGoiMon = null;
	private JTabbedPane panelKVB = null;
	public MyItemThongTinGoiMon(TTGoiMon value, JPanel panelGoiMon, JTabbedPane kvb) {
		super();
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(arg0.getButton() == MouseEvent.BUTTON1 && arg0.getClickCount() == 2)
					XuatMon();
			}
		});
		
		setData(value);
		this.panelGoiMon = panelGoiMon;
		this.panelKVB = kvb;
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(this, popupMenu);
		
		JMenuItem mntmXutMn = new JMenuItem("Xuất món");
		mntmXutMn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				XuatMon();
			}
		});
		popupMenu.add(mntmXutMn);
		
		JMenuItem mntmHuMn = new JMenuItem("Huỷ món");
		mntmHuMn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				HuyMon();
			}
		});
		popupMenu.add(mntmHuMn);
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	public void XuatMon()
	{
		this.entry.setDapUng(true);
		new TTGoiMonDAO().saveOrUpdate(this.entry);
		this.panelGoiMon.remove(this);
		this.panelGoiMon.updateUI();
	}
	public void HuyMon()
	{
		try {
			int res =  JOptionPane.showConfirmDialog(this.panelGoiMon, "Bạn có chắc muốn huỷ món "+ this.entry.getMon().getTen()+" của "+this.entry.getBan().getTen() +" không?");
			if(res == JOptionPane.OK_OPTION)
			{
				/////////cập nhật lượng nguyên liệu
				
				Ban b = this.entry.getBan();
				//xoa thong tin goi mon
				TTGoiMonDAO dao = new TTGoiMonDAO();
				dao.delete(this.entry);
				
				// cap nhat giao dien
				this.panelGoiMon.remove(this);
				this.panelGoiMon.updateUI();
				
				// cap nhat tinh trang ban
				int c = dao.DemSoMonGoi(b.getMaSo());
				
				BanDAO daoB =  new BanDAO();
				MyControl ctrl = new MyControl();
				if(c == 0)
				{
					//cap nhat csdl
					b.setTrangThai(false);
					daoB.saveOrUpdate(b);
					
					//cap nhat giao dien
					ctrl.DonBan(b, panelKVB, this.panelGoiMon);
				}
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this.panelGoiMon, "không thực hiện được!!!","Lỗi",JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public TTGoiMon getData() {
		return entry;
	}
	public void setData(TTGoiMon value) {
		this.entry = value;
		
		this.setIcon(entry.getMon().getImageIcon());
		this.setToolTipText(entry.getMon().getTen()+", bàn gọi: "+entry.getBan().getTen()+", thời gian gọi: "+Common.GetTimeString(entry.getTgGoi(),"dd/MM/yyyy HH:mm:ss"));
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
